home *** CD-ROM | disk | FTP | other *** search
/ Aminet 52 / Aminet 52 (2002)(GTI - Schatztruhe)[!][Dec 2002].iso / Aminet / dev / gg / gengetopt-2.6.lha / gengetopt-2.6 / src / cmdline.c < prev    next >
Text File  |  2002-03-02  |  8KB  |  256 lines

  1. /*
  2.   File autogenerated by gengetopt version 2.6  
  3.   generated with the following command:
  4.   ../src/gengetopt --input=cmdline.ggo --no-handle-version --no-handle-help --no-handle-error 
  5.  
  6.   The developers of gengetopt consider the fixed text that goes in all
  7.   gengetopt output files to be in the public domain:
  8.   we make no copyright claims on it.
  9. */
  10.  
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. /* If we use autoconf.  */
  16. #ifdef HAVE_CONFIG_H
  17. #include "config.h"
  18. #endif
  19. /* Check for configure's getopt check result.  */
  20. #ifndef HAVE_GETOPT_LONG
  21. #include "getopt.h"
  22. #else
  23. #include <getopt.h>
  24. #endif
  25.  
  26. #ifndef HAVE_STRDUP
  27. #define strdup gengetopt_strdup
  28. #endif /* HAVE_STRDUP */
  29.  
  30. #include "cmdline.h"
  31.  
  32.  
  33. void
  34. cmdline_parser_print_version (void)
  35. {
  36.   printf ("%s %s\n", PACKAGE, VERSION);
  37. }
  38.  
  39. void
  40. cmdline_parser_print_help (void)
  41. {
  42.   cmdline_parser_print_version ();
  43.   printf("\n"
  44. "Purpose:\n"
  45. "  This program generates a C function that uses getopt_long function\n"
  46. "  to parse the command line options, validate them and fill a struct.\n"
  47. "\n"
  48. "Usage: %s [OPTIONS]...\n", PACKAGE);
  49.   printf("   -h         --help                Print help and exit\n");
  50.   printf("   -V         --version             Print version and exit\n");
  51.   printf("   -iSTRING   --input=STRING        input file (default std input)\n");
  52.   printf("   -fSTRING   --func-name=STRING    name of generated function (default='cmdline_parser')\n");
  53.   printf("   -FSTRING   --file-name=STRING    name of generated file (default='cmdline')\n");
  54.   printf("   -l         --long-help           long usage line in help\n");
  55.   printf("   -u         --unamed-opts         accept filenames\n");
  56.   printf("              --no-handle-help      do not handle --help|-h automatically\n");
  57.   printf("              --no-handle-version   do not handle --version|-V automatically\n");
  58.   printf("              --no-handle-error     do not exit on errors\n");
  59. }
  60.  
  61.  
  62. /* gengetopt_strdup(): automatically generated from strdup.c. */
  63. /* strdup.c replacement of strdup, which is not standard */
  64. static char *
  65. gengetopt_strdup (const char *s)
  66. {
  67.   char *result = (char*)malloc(strlen(s) + 1);
  68.   if (result == (char*)0)
  69.     return (char*)0;
  70.   strcpy(result, s);
  71.   return result;
  72. }
  73.  
  74. int
  75. cmdline_parser (int argc, char * const *argv, struct gengetopt_args_info *args_info)
  76. {
  77.   int c;    /* Character of the parsed option.  */
  78.   int missing_required_options = 0;    
  79.  
  80.   args_info->help_given = 0 ;
  81.   args_info->version_given = 0 ;
  82.   args_info->input_given = 0 ;
  83.   args_info->func_name_given = 0 ;
  84.   args_info->file_name_given = 0 ;
  85.   args_info->long_help_given = 0 ;
  86.   args_info->unamed_opts_given = 0 ;
  87.   args_info->no_handle_help_given = 0 ;
  88.   args_info->no_handle_version_given = 0 ;
  89.   args_info->no_handle_error_given = 0 ;
  90. #define clear_args() { \
  91.   args_info->input_arg = NULL; \
  92.   args_info->func_name_arg = strdup("cmdline_parser") ;\
  93.   args_info->file_name_arg = strdup("cmdline") ;\
  94. }
  95.  
  96.   clear_args();
  97.  
  98.   optarg = 0;
  99.   optind = 1;
  100.   opterr = 1;
  101.   optopt = '?';
  102.  
  103.   while (1)
  104.     {
  105.       int option_index = 0;
  106.       static struct option long_options[] = {
  107.         { "help",    0, NULL, 'h' },
  108.         { "version",    0, NULL, 'V' },
  109.         { "input",    1, NULL, 'i' },
  110.         { "func-name",    1, NULL, 'f' },
  111.         { "file-name",    1, NULL, 'F' },
  112.         { "long-help",    0, NULL, 'l' },
  113.         { "unamed-opts",    0, NULL, 'u' },
  114.         { "no-handle-help",    0, NULL, 0 },
  115.         { "no-handle-version",    0, NULL, 0 },
  116.         { "no-handle-error",    0, NULL, 0 },
  117.         { NULL,    0, NULL, 0 }
  118.       };
  119.  
  120.       c = getopt_long (argc, argv, "hVi:f:F:lu", long_options, &option_index);
  121.  
  122.       if (c == -1) break;    /* Exit from `while (1)' loop.  */
  123.  
  124.       switch (c)
  125.         {
  126.         case 'h':    /* Print help and exit.  */
  127.           if (args_info->help_given)
  128.             {
  129.               fprintf (stderr, "%s: `--help' (`-h') option given more than once\n", PACKAGE);
  130.               clear_args ();
  131.               return (EXIT_FAILURE);
  132.             }
  133.           args_info->help_given = 1;
  134.           return 0;
  135.  
  136.         case 'V':    /* Print version and exit.  */
  137.           if (args_info->version_given)
  138.             {
  139.               fprintf (stderr, "%s: `--version' (`-V') option given more than once\n", PACKAGE);
  140.               clear_args ();
  141.               return (EXIT_FAILURE);
  142.             }
  143.           args_info->version_given = 1;
  144.           return 0;
  145.  
  146.         case 'i':    /* input file (default std input).  */
  147.           if (args_info->input_given)
  148.             {
  149.               fprintf (stderr, "%s: `--input' (`-i') option given more than once\n", PACKAGE);
  150.               clear_args ();
  151.               return (EXIT_FAILURE);
  152.             }
  153.           args_info->input_given = 1;
  154.           args_info->input_arg = strdup (optarg);
  155.           break;
  156.  
  157.         case 'f':    /* name of generated function.  */
  158.           if (args_info->func_name_given)
  159.             {
  160.               fprintf (stderr, "%s: `--func-name' (`-f') option given more than once\n", PACKAGE);
  161.               clear_args ();
  162.               return (EXIT_FAILURE);
  163.             }
  164.           args_info->func_name_given = 1;
  165.           args_info->func_name_arg = strdup (optarg);
  166.           break;
  167.  
  168.         case 'F':    /* name of generated file.  */
  169.           if (args_info->file_name_given)
  170.             {
  171.               fprintf (stderr, "%s: `--file-name' (`-F') option given more than once\n", PACKAGE);
  172.               clear_args ();
  173.               return (EXIT_FAILURE);
  174.             }
  175.           args_info->file_name_given = 1;
  176.           args_info->file_name_arg = strdup (optarg);
  177.           break;
  178.  
  179.         case 'l':    /* long usage line in help.  */
  180.           if (args_info->long_help_given)
  181.             {
  182.               fprintf (stderr, "%s: `--long-help' (`-l') option given more than once\n", PACKAGE);
  183.               clear_args ();
  184.               return (EXIT_FAILURE);
  185.             }
  186.           args_info->long_help_given = 1;
  187.           break;
  188.  
  189.         case 'u':    /* accept filenames.  */
  190.           if (args_info->unamed_opts_given)
  191.             {
  192.               fprintf (stderr, "%s: `--unamed-opts' (`-u') option given more than once\n", PACKAGE);
  193.               clear_args ();
  194.               return (EXIT_FAILURE);
  195.             }
  196.           args_info->unamed_opts_given = 1;
  197.           break;
  198.  
  199.         
  200.         
  201.         
  202.         case 0:    /* Long option with no short option */
  203.           /* do not handle --help|-h automatically.  */
  204.           if (strcmp (long_options[option_index].name, "no-handle-help") == 0)
  205.           {
  206.             if (args_info->no_handle_help_given)
  207.               {
  208.                 fprintf (stderr, "%s: `--no-handle-help' option given more than once\n", PACKAGE);
  209.                 clear_args ();
  210.                 return (EXIT_FAILURE);
  211.               }
  212.             args_info->no_handle_help_given = 1;
  213.             break;
  214.           }
  215.           /* do not handle --version|-V automatically.  */
  216.           else if (strcmp (long_options[option_index].name, "no-handle-version") == 0)
  217.           {
  218.             if (args_info->no_handle_version_given)
  219.               {
  220.                 fprintf (stderr, "%s: `--no-handle-version' option given more than once\n", PACKAGE);
  221.                 clear_args ();
  222.                 return (EXIT_FAILURE);
  223.               }
  224.             args_info->no_handle_version_given = 1;
  225.             break;
  226.           }
  227.           /* do not exit on errors.  */
  228.           else if (strcmp (long_options[option_index].name, "no-handle-error") == 0)
  229.           {
  230.             if (args_info->no_handle_error_given)
  231.               {
  232.                 fprintf (stderr, "%s: `--no-handle-error' option given more than once\n", PACKAGE);
  233.                 clear_args ();
  234.                 return (EXIT_FAILURE);
  235.               }
  236.             args_info->no_handle_error_given = 1;
  237.             break;
  238.           }
  239.  
  240.         case '?':    /* Invalid option.  */
  241.           /* `getopt_long' already printed an error message.  */
  242.           return (EXIT_FAILURE);
  243.  
  244.         default:    /* bug: option not considered.  */
  245.           fprintf (stderr, "%s: option unknown: %c\n", PACKAGE, c);
  246.           abort ();
  247.         } /* switch */
  248.     } /* while */
  249.  
  250.   if ( missing_required_options )
  251.     return (EXIT_FAILURE);
  252.  
  253.  
  254.   return 0;
  255. }
  256.